package com.todod.base.module.base.service.datadistribute;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.base.controller.admin.datadistribute.vo.*;
import com.todod.base.module.base.dal.dataobject.datadistribute.DataDistributeDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.datadistribute.DataDistributeMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 数据分发设置 Service 实现类
 *
 * @author 超管儿
 */
@Service
@Validated
public class DataDistributeServiceImpl implements DataDistributeService {

    @Resource
    private DataDistributeMapper dataDistributeMapper;

    @Override
    public Long createDataDistribute(DataDistributeSaveReqVO createReqVO) {
        // 插入
        DataDistributeDO dataDistribute = BeanUtils.toBean(createReqVO, DataDistributeDO.class);
        dataDistributeMapper.insert(dataDistribute);
        // 返回
        return dataDistribute.getId();
    }

    @Override
    public void updateDataDistribute(DataDistributeSaveReqVO updateReqVO) {
        // 校验存在
        validateDataDistributeExists(updateReqVO.getId());

        if(updateReqVO.getIsSend().equals(0)){
            updateReqVO.setIpAddress("");
        }

        // 更新
        DataDistributeDO updateObj = BeanUtils.toBean(updateReqVO, DataDistributeDO.class);
        dataDistributeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataDistribute(Long id) {
        // 校验存在
        validateDataDistributeExists(id);
        // 删除
        dataDistributeMapper.deleteById(id);
    }

    private void validateDataDistributeExists(Long id) {
        if (dataDistributeMapper.selectById(id) == null) {
            throw exception(DATA_DISTRIBUTE_NOT_EXISTS);
        }
    }

    @Override
    public DataDistributeDO getDataDistribute(Long id) {
        return dataDistributeMapper.selectById(id);
    }

    @Override
    public PageResult<DataDistributeDO> getDataDistributePage(DataDistributePageReqVO pageReqVO) {
        return dataDistributeMapper.selectPage(pageReqVO);
    }

}